﻿using System;
using System.Collections.Generic;
using System.Text;
using CFSystem.JXC.IDAL;
using CFSystem.JXC.Model;
using System.Data;
using System.Data.OleDb;
using DBUtility;

namespace CFSystem.JXC.AccessDAL
{
    public class Unit : IUnit
    {

        public Unit()
        { }

        /// <summary>
        /// Method to get all UnitInfo
        /// </summary>		
        /// <returns>Interface to Model Collection Generic of Units</returns>
        public IList<UnitInfo> GetUnits()
        {

            string sqlSelect = "SELECT * from tb_Unit";

            OleDbDataReader dr = AccessHelper.ExecuteReader(AccessHelper.ConnectionString, CommandType.Text, sqlSelect);

            IList<UnitInfo> UnitItems = new List<UnitInfo>();

            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    UnitInfo bi = new UnitInfo(Convert.ToInt32(dr[0].ToString()), Convert.ToString(dr[1].ToString()));
                    UnitItems.Add(bi);
                }
                dr.Close();
            }
            return UnitItems;
        }

        /// <summary>
        /// Get information on a specific Unit
        /// </summary>
        /// <param name="categoryId">Unique identifier for a Unit</param>
        /// <returns>Business Entity representing an Unit</returns>
        public UnitInfo GetUnit(string UnitID)
        {
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="UnitName"></param>
        public void InsertUnit(string UnitName)
        {
            string sqlInsert = "insert into [tb_Unit]([Name]) values('" + UnitName + "')";

            int returnCnt = AccessHelper.ExecuteNonQuery(AccessHelper.ConnectionString, CommandType.Text, sqlInsert);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="UnitID"></param>
        public void DelUnitByID(int UnitID)
        {
            string sqlDel = "delete from [tb_Unit] where [UnitID]=" + UnitID;

            int returnCnt = AccessHelper.ExecuteNonQuery(AccessHelper.ConnectionString, CommandType.Text, sqlDel);

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="UnitID"></param>
        /// <param name="updValue"></param>
        public void UpdateUnitByID(int UnitID, string updValue)
        {
            string sqlUpd = "update [tb_Unit] set [Name] ='" + updValue + "'" + " where [UnitID]=" + UnitID;

            int returnCnt = AccessHelper.ExecuteNonQuery(AccessHelper.ConnectionString, CommandType.Text, sqlUpd);

        }
    }
}
